package cn.xorange.commons.security.configuration;

import cn.xorange.commons.security.interceptor.ResponseInterceptor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author yangjian
 */
@Configuration
public class WebConfigure implements WebMvcConfigurer {

    @Resource
    ResponseInterceptor webInterceptor;
    @Resource
    IgnoreUrlConfigure ignoreUrlConfigure;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
//        /** 本地文件上传路径 */
//        registry.addResourceHandler(FileConstants.LOCAL_RESOURCE_PREFIX + "/**")
//                .addResourceLocations("file:" + uploadConfig.basePath() + File.separator);

        /** swagger配置 */
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("druid/login.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webInterceptor)
                .addPathPatterns("/**").excludePathPatterns(ignoreUrlConfigure.getIgnoreStaticUrls());
    }

}
